package com.zpark.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Response<T> {
    /**
     * 单个数据对象
     */
    private T       data;
    /**
     * 数据集合
     */
    private List<T> rows;
    /**
     * 总数，分页查询时的总条数
     */
    private Long    total;
    /**
     * 响应码
     */
    private String  code;
    /**
     * 响应信息
     */
    private String  message;

    public Response(Code code){
        this.code=code.getValue();
        this.message=code.getMessage();
    }

    /**
     * 执行成功
     * @return
     */
    public static Response success(){
        return new Response(Code.SUCCESS);
    }

    public static <V> Response<V> success(V value){
        return new Response<V>(Code.SUCCESS).setData(value);
    }

    public static <V> Response<V> success(List<V> value,Long total){
        return new Response<V>(Code.SUCCESS).setRows(value).setTotal(total);
    }

    /**
     * 执行失败
     * @return
     */
    public static Response failure(){
        return new Response(Code.FAILURE);
    }
}
